﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace SharingCenter.Web.Mod.Learning
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Requests;
    using SharingCenter.Web.Classes.Learning;
    using System.Xml.Linq;
    using System.Data.Linq;
    using SharingCenter.Web.Classes.Messages;
    using System.Xml;
    /// <summary>
    /// Summary description for LearningWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class LearningWebService : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod]
        public void ApproveLearningRequest(string requestId)
        {
            Guid rid = new Guid(requestId);
            RequestOperations.ChangeRequestStatus(rid, RequestStatus.Approved, DateTime.Now);
            Sc_SharedRequest request = RequestOperations.GetRequestById(rid);
            Guid hisid = LearningOperations.CreateLearningHistoryOnApproval(request, false);
            XElement x = request.Context;
            x.Add(new XElement("LearningHistoryId", hisid));
            request.Context = new XElement(x);
            
            scDataContext.Instance.SubmitChanges(ConflictMode.FailOnFirstConflict);
        }

        [WebMethod]
        [ScriptMethod]
        protected void SendApproveMessage(Sc_SharedRequest request)
        {
            //XElement cxt = new XElement("Context",
            //        new XAttribute("XsltFolderInMod", "Learning"),
            //        new XElement("Event",
            //            new XAttribute("Name", "LearningRequestApproved"),
            //            new XElement("LearningHistoryId", his.LearningHistoryId),
            //            new XElement("Comment", comment),
            //            new XElement("Score", score)));
            //MessageOperations.SendSystemMessage(request.RequestorUserId, request.RequestorUserId, "LearningRequestApproved", 
        }

        [WebMethod]
        [ScriptMethod]
        public void RejectLearingRequest(string requestId)
        {
            Guid rid = new Guid(requestId);
            RequestOperations.ChangeRequestStatus(rid, RequestStatus.Rejected, DateTime.Now);
        }

        //[WebMethod]
        //[ScriptMethod]
        //public AjaxPro.IJavaScriptObject GetCategoryList()
        //{
        //    //XDocument doc = XDocument.Load(Server.MapPath(WebConsts.APPDATA_DIR + "/Skills.xml"));
        //    XmlDocument doc = new XmlDocument();
        //    doc.Load(Server.MapPath(WebConsts.APPDATA_DIR + "/Skills.xml"));
        //    return AjaxPro.JavaScriptUtil.GetIJavaScriptObjectFromXmlNode(doc.DocumentElement);
        //}

        [WebMethod]
        [ScriptMethod]
        public bool SendLearningRequest(string requesteeLpId, string requesteeUserId, string requestorLpId, string requestorUserId, string comment)
        {
            try
            {
                comment = Server.HtmlEncode(comment);
                Guid requestee = new Guid(requesteeUserId);
                Guid requestor = new Guid(requestorUserId);
                Guid rqsteeLpId = new Guid(requesteeLpId);
                Guid rqstorLpId = new Guid(requestorLpId);

                LearningRequestContext cxt = new LearningRequestContext();
                cxt.Comment = comment;
                cxt.RequesteeProfileId = rqsteeLpId;
                cxt.RequesteeUserId = requestee;
                cxt.RequestorProfileId = rqstorLpId;
                cxt.RequestorUserId = requestor;                

                Sc_SharedRequest req = RequestOperations.AddRequest(EntityType.Learning, cxt.RequesteeProfileId, cxt.RequestorUserId, cxt.RequesteeUserId, cxt.Serialize());
                SendApproveMessage(req);
                //RequestOperations.ChangeRequestStatus(rid, RequestStatus.Rejected, true);
                return true;
            }
            catch { return false; }
        }
    }
}
